<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <link href="css/primer.css" rel="stylesheet" />
  <link rel="stylesheet" href="css/basic.css" />
  <script src="Js/md5.js"></script>
  <script src="http://cdn.staticfile.org/jquery/2.0.0/jquery.min.js"></script>
  <script src="https://cdn.bootcdn.net/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.js"></script>
  <script src="Js/axform.js"></script>
  <title>EZBabyCTF-出题</title>
  <style>
    .upbox {
      width: 1024px;
      position: absolute;
      left: 50%;
      transform: translateX(-50%);
      text-align: center;
      font-size: large;
      font-weight: bold;
    }


    .upchallenge {
      height: 530px;
      display: none;
    }

    .Box-row {
      position: relative;
      height: 8vh;
    }

    .challname {
      position: absolute;
      left: 15%;
    }

    .challtype {
      display: none;
    }

    .rmchll {
      position: absolute;
      right: 15%;
      top: 20%;
    }

    .rmchll:hover {
      background-color: rgba(255, 0, 0, 0.826);
    }

    #topic {
      position: absolute;
      top: 20%;
      left: 10%;
    }

    #editor1 {
      height: 61%;
    }

    #discibe {
      position: absolute;
      top: 40%;
      left: 10%;
    }

    #setscore {
      position: absolute;
      top: 70%;
      left: 10%;
    }

    #setflag {
      position: absolute;
      top: 20%;
      right: 10%;
    }

    #settype {
      position: absolute;
      top: 45%;
      right: 12.5%;
    }

    #setfile {
      position: absolute;
      top: 70%;
      right: -4.3%;
    }

    #Sbmit {
      position: absolute;
      left: 50%;
      top: 85%;
      width: 10%;
      transform: translate(-50%);
    }

    textarea {
      width: 213px;
      resize: none;
    }
  </style>
</head>

<body>
  <div class="Header" style="background-color: rgba(0, 0, 0, 0.826)">
    <div class="Header-item m-auto">
      <span id="Head">EZBabyCTF上传题目</span>
    </div>
    <div class="Header-item m-3">
      <span class="Header-link" id="returnindex">返回首页</span>
    </div>
  </div>
  <div class="Box anim-fade-in upbox" id="chllfa">
    <div class="Box-header Link" id="submitchallenge">上传题目/修改题目</div>
    <div class="Box-body upchallenge">
      <div id="render1" class="Box-body"><button class="btn-mktg btn-transparent enlarge m-2" type="button"
          style="position: relative">
          <div class="challengename">EZBabyWEB</div>
          <div class="challengedisc">
            题目描述：
          </div>
          <span class="score">1000</span>
          <div class="submitflagfather">
            <input class="form-control submitflag" type="text" placeholder="flag{xxxxxxx}" />
            <span class="Box-btn-octicon btn-octicon submittingbtn" type="button"><svg class="octicon octicon-x"
                xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" width="16" height="16">
                <path fill-rule="evenodd"
                  d="M8.53 1.22a.75.75 0 00-1.06 0L3.72 4.97a.75.75 0 001.06 1.06l2.47-2.47v6.69a.75.75 0 001.5 0V3.56l2.47 2.47a.75.75 0 101.06-1.06L8.53 1.22zM3.75 13a.75.75 0 000 1.5h8.5a.75.75 0 000-1.5h-8.5z">
                </path>
              </svg></span>
          </div>
        </button>
      </div>
      <div class="Box-body" id="editor1" style="position: relative;">
        <form action="admin/add.php" method="post" enctype="multipart/form-data"
          onsubmit="if(event.keyCode==13){return false;}">
          <div id="topic">
            <span>
              题目名称：
            </span>
            <span>
              <input class="form-control" id="chaname" type="text" name="chall_name" onblur="render()"
                placeholder="题目名称请与上传文件名相同">
            </span>
          </div>
          <div id="discibe">
            <span>
              题目描述：
            </span>
            <span>
              <textarea class="form-control" id="area" name="chall_des" onblur="render()"
                placeholder="题目描述不要超过56个字"></textarea>
            </span>
          </div>
          <div id="setscore">
            <span>
              题目分值：
            </span>
            <span>
              <input class="form-control" id="chascore" type="number" onblur="render()" name="chall_score"
                placeholder="1000">
            </span>
          </div>
          <div id="setflag">
            <span>
              题目Flag：
            </span>
            <span>
              <input class="form-control" type="text" name="chall_flag" onblur="render()"
                placeholder="flag{xxxxxxxxxx}">
            </span>
          </div>
          <div id="settype" onblur="render()">
            <span>
              题目类型：
            </span>
            <span>
              <label> <input type="radio" name="chall_type" value="Misc" checked /> Misc </label>
              <label> <input type="radio" name="chall_type" value="Web" /> Web </label>
              <label> <input type="radio" name="chall_type" value="Re" /> Re </label>
            </span>
          </div>
          <div id="setfile">
            <span>
              <label for="file">上传文件：</label>
              <input type="file" name="chall_file" id="file" onblur="render()">
            </span>
          </div>

          <button type="submit" id="Sbmit" class="btn btn-primary">提交</button>
        </form>
      </div>
    </div>
    <div class="Box-body">如要修改题目请用新题直接覆盖</div>
  </div>


  <script>
    $(document).ready(function () {
      if ($.cookie("salt") == undefined) {
        window.alert("error 3");
        window.location.href = "index.html";
      }
      else {
        var id = $.cookie("salt").substring(0, 32);
        $.ajax({
          url: "team.php",
          type: "GET",
          async: false,
          dataType: "json",
          data: {
            id: id
          },
          success: function (response) {
            console.log(response);
            $.each(response, function (m1, m2) {
              if (m2 != "__ADMIN__" && m1 === 0) {
                window.location = "index.html";
              }
            })
          },
          error: function () {
            window.alert("error:1");
          }
        });
        $.ajax({
          url: "show_challenges.php",
          type: "GET",
          async: false,
          dataType: "json",
          success: function (challinfo) {
            var toappdf = $("#chllfa");
            var trashbar = '<button class="Box-btn-octicon btn-octicon rmchll" type="button"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24"><path fill-rule="evenodd" d="M16 1.75V3h5.25a.75.75 0 010 1.5H2.75a.75.75 0 010-1.5H8V1.75C8 .784 8.784 0 9.75 0h4.5C15.216 0 16 .784 16 1.75zm-6.5 0a.25.25 0 01.25-.25h4.5a.25.25 0 01.25.25V3h-5V1.75z"></path><path d="M4.997 6.178a.75.75 0 10-1.493.144L4.916 20.92a1.75 1.75 0 001.742 1.58h10.684a1.75 1.75 0 001.742-1.581l1.413-14.597a.75.75 0 00-1.494-.144l-1.412 14.596a.25.25 0 01-.249.226H6.658a.25.25 0 01-.249-.226L4.997 6.178z"></path><path d="M9.206 7.501a.75.75 0 01.793.705l.5 8.5A.75.75 0 119 16.794l-.5-8.5a.75.75 0 01.705-.793zm6.293.793A.75.75 0 1014 8.206l-.5 8.5a.75.75 0 001.498.088l.5-8.5z"></path></svg></button>'
            $.each(challinfo, function (m1, m2) {
              toappdf.append('<div class="Box-row"><span class="challname">' + m2[1] + '</span><span class="challtype">' + m2[2] + '</span>' + trashbar + '</div>');
            })
          }

        })
      }
      $("form").ajaxForm(function (data) {
        console.log(data);
        if (data[0] + data[1] + "" == '10' || data[0] == '6') {
          window.alert("题目上传成功！请返回首页查看");
        }
        else {
          window.alert("检查一下！");
        }
      }
      );
      $("#returnindex").click(function () {
        window.location.href = "index.html";
      })
      $("#submitchallenge").click(function () {
        $(".upchallenge").toggle();
      })
      render();
      $(".rmchll").click(function () {
        var chname = $(this).parent().find(".challname").text();
        var chtype = $(this).parent().find(".challtype").text();
        // console.log(chname);
        // console.log(chtype);
        $.ajax({
          url: "delete_challenge.php",
          type: "POST",
          dataType: "text",
          data: {
            chall_type: chtype,
            chall_name: chname
          },
          success: function (res) {
            if (res == 'TRUE') {
              window.alert("已删除成功！");
            }
            else {
              window.alert("删除失败！");
            }
          },
          error: function () {
            window.alert("error:1");
          }
        })
      })
    })
    document.onkeydown = function (event) {
      var target, code, tag;
      if (!event) {
        event = window.event; //针对ie浏览器
        target = event.srcElement;
        code = event.keyCode;
        if (code == 13) {
          tag = target.tagName;
          if (tag == "TEXTAREA") { return true; }
          else { return false; }
        }
      }
      else {
        target = event.target;
        code = event.keyCode;
        if (code == 13) {
          tag = target.tagName;
          if (tag == "INPUT") { return false; }
          else { return true; }
        }
      }
    };
    function render() {
      $(".challengename").text($("#chaname").val());
      $(".challengedisc").text($("#area").val());
      $(".score").text($("#chascore").val());
    }
  </script>
</body>

</html>